package com.kdjack.analogInput.main;

import com.kdjack.analogInput.service.PressKey;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * @author KDJack
 * @date 2020/2/18 19:47
 */
public class print implements ActionListener {

    public static void main(String[] args) {
        // 第一步，注册监听的KEY
        JIntellitype.getInstance().registerHotKey(0, 0, KeyEvent.VK_F8);

        // 第二步：添加热键监听器
        JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
            public void onHotKey(int markCode) {
                try {
                    Robot robot = new Robot();
                    // 读取文本内容;
                    String fileName = "E:\\input\\Test.java";
                    FileReader fileReader = new FileReader(fileName);

                    BufferedReader bufferedReader = new BufferedReader(fileReader);
                    String line = bufferedReader.readLine();
                    // notepad++,记事本版本
                    while (line != null) {
                        // System.out.println(line);
                        for (int i = 0; i < line.length(); ++i) {
                            PressKey.pressKey(robot, line.charAt(i));
                        }
                        // 换行
                        PressKey.pressKey(robot, KeyEvent.VK_ENTER);
                        // 继续读
                        line = bufferedReader.readLine();
                    }

                    // idea版本--如果光标在idea里面
                /*while (line != null) {
                    line = line.replace("  ", "");
                    System.out.println(line);
                    // 花括号结尾
                    for (int i = 0; i < line.length(); ++i) {
                        if (line.charAt(i) == '}') {
                            // ↓键
                            PressKey.pressKey(robot, KeyEvent.VK_DOWN);
                        } else {
                            PressKey.pressKey(robot, line.charAt(i));
                        }
                    }
                    if (line.length() <= 0 || line.charAt(line.length() - 1) != '}') {
                        // 换行
                        PressKey.pressKey(robot, KeyEvent.VK_ENTER);
                    }
                    // 继续读
                    line = bufferedReader.readLine();
                }*/
                    bufferedReader.close();
                    fileReader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
    }

}
